/*
 * Created on 07.12.2003
 */
package observations;

import java.util.*;

/**
 <code>Registrar</code> Manages saving an retreiving to/from memory.
 * Registrar is a singleton.
 
 @author Sascha Hemminger
 @version 2004-09-10
 */
public class Registrar {

  private static Registrar soleInstance = new Registrar();

  /**
   * Adds an object to the registry (saves it).
   
   @param entryPoint
   *            type of the object (eg. range or phenomenon)
   @param newObject
   *            the object to save
   */
  public static void add(String entryPoint, DomainObject newObject) {
    soleInstance.addObject(entryPoint, newObject);
  }

  /**
   * Retrieves objects from the registry.
   
   @param entryPointName
   *            type of the object
   @param objectName
   *            object's name
   @return the object
   */
  public static DomainObject get(String entryPointName, String objectName) {
    return soleInstance.getObject(entryPointName, objectName);
  }

  private Map entryPoints = new Hashtable();

  /*
   * Notice: Die von Fowler benutzte abstrakte Klasse java.util.Dictionary ist
   * obsolet. Stattdessen wurde hier java.util.Map verwendet.
   */
  private void addObject(String entryPointName, DomainObject newObject) {
    Map theEntryPoint = (MapentryPoints.get(entryPointName);
    if (theEntryPoint == null) {
      theEntryPoint = new Hashtable();
      entryPoints.put(entryPointName, theEntryPoint);
    }
    theEntryPoint.put(newObject.getName(), newObject);
  }

  private void assertNonNull(Object arg, String message) {
    if (arg == null)
      throw new NullPointerException(message);
  }

  private DomainObject getObject(String entryPointName, String objectName) {
    Map theEntryPoint = (MapentryPoints.get(entryPointName);
    assertNonNull(theEntryPoint, "No entry point present for "
        + entryPointName);
    DomainObject answer = (DomainObjecttheEntryPoint.get(objectName);
    assertNonNull(answer, "There is no " + entryPointName + " called "
        + objectName);
    return answer;
  }
}